//
//  RadioRootViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-4.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "RadioRootViewController.h"
#import "OneTableViewCell.h"
#import "TwoTableViewCell.h"

#import "RadioHandel.h"
#import "RadioDataModel.h"
#import "UIImage+ImageDown.h"

#pragma mark 详情页面的model类;
#import "radioDetailDataModel.h"
#import "radioDetailView.h"

#import "radioDetailViewController.h"
#import "RadioTableView.h"


#pragma mark 下拉刷新
//#import "UITableView+DragLoad.h"
#import "MJRefresh.h"

#define KFrame  CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height);

@interface RadioRootViewController () <UITableViewDelegate,UITableViewDataSource>
//遵循下拉代理;

@property(nonatomic, retain)RadioTableView *radioTableView;

@end

@implementation RadioRootViewController

- (void)dealloc
{
    [_radioTableView release];
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.radioTableView = [[[RadioTableView alloc] init] autorelease];
        self.radioTableView.frame = KFrame;
        
        
    }
    return self;
}

- (void)loadView
{
    self.view = self.radioTableView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   
    //
    // self.radioTableView.frame = KFrame;
    
    [RadioHandel shareRadioData].JKcount = 0;
    
    self.radioTableView.delegate = self;
    self.radioTableView.dataSource = self;
    
       //注册大cell
    [self.radioTableView registerClass:[OneTableViewCell class] forCellReuseIdentifier:@"oneCell"];
    //注册小cell;
    [self.radioTableView registerClass:[TwoTableViewCell class] forCellReuseIdentifier:@"twoCell"];
    //请求radio的数据;
    [[RadioHandel shareRadioData] reqestRadioData];
    //kvo
    [[RadioHandel shareRadioData] addObserver:self forKeyPath:@"RadioTotalArray" options:(NSKeyValueObservingOptionNew) context:nil];

    [self.radioTableView  addHeaderWithTarget:self action:@selector(dragTableDidTriggerRefresh:)];
    [self.radioTableView addFooterWithTarget:self action:@selector(dragTableDidTriggerLoadMore:) ];
    
}



-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"RadioTotalArray"]) {
        [self.radioTableView reloadData];
     [[RadioHandel shareRadioData] removeObserver:self forKeyPath:@"RadioTotalArray" context:nil];
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //注释 :这里的返回值最后需要根据数据的不同判断用哪一个高度,或者使用自适应高度;
    //先给一个大cell的死值180;
    if (indexPath.row == 0) {
        return 180;
    }else{
        //给小cell一个死值90;
        return 90;
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [RadioHandel shareRadioData].RadioTotalArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[RadioHandel shareRadioData].RadioTotalArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioDataModel *rdm = [RadioHandel shareRadioData].RadioTotalArray[indexPath.section][indexPath.row];

 #pragma mark  这里的docid 就是cell详情页面借口要用的;
    //测试借口中的参数有没有
    if (indexPath.row ==0) {
        //大cell
        OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneCell" forIndexPath:indexPath];
        cell.titlelab.text = rdm.title;
        
        NSLog(@"%@",rdm.imgsrc);
        
        [cell.imv  setImage:[UIImage imageWithUrlString:rdm.imgsrc myBlock:^(UIImage *image) {
            cell.imv.image = image;
        }]];
        return cell;
    }else{
        //这是小cell:
         TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"twoCell" forIndexPath:indexPath];
        
         cell.titlelab.text = rdm.title;

         cell.imv.image = [UIImage imageWithUrlString:rdm.imgsrc myBlock:^(UIImage *image) {
             
           cell.imv.image = image;
             
         }];
        
         cell.typelab.text = rdm.source;
         cell.sizelab.text = rdm.size;
         return cell;
    }
}

#pragma mark 点击进入详情页面  并且在这里给它传入值
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    //必须得先传参再入栈 ,进行模态跳转.

    //给单利中的docid属性赋值.
    RadioDataModel *rdm = [RadioHandel shareRadioData].RadioTotalArray[indexPath.section][indexPath.row];
    
  
    [RadioHandel shareRadioData].docidString = rdm.docid;
    NSLog( @"观察不同docid::%@",rdm.docid);
    
    radioDetailViewController *radioVC = [[[radioDetailViewController alloc] init] autorelease];
    
    [self.navigationController presentViewController:radioVC animated:YES completion:nil];
    
    NSLog(@"cell%@",[RadioHandel shareRadioData].docidString);
    
       
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
   
}

#pragma mark 实现下拉刷新的方法
//Called when table trigger a refresh event.
- (void)dragTableDidTriggerRefresh:(UITableView *)tableView
{
    NSLog(@"下拉刷新");
    
    
        [[RadioHandel shareRadioData] reqestRadioData];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
   
        [self.radioTableView headerEndRefreshing];
        
        [self.radioTableView reloadData ];
        
    });
}

- (void)dragTableDidTriggerLoadMore:(UITableView *)tableView
{
    NSLog(@"上拉加载");
    
    
    [RadioHandel shareRadioData ].JKcount +=20;

    [[RadioHandel shareRadioData] reqestRadioData];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
        [self.radioTableView footerEndRefreshing];
        
        [self.radioTableView reloadData ];
        
    });
  
}





@end
